package com.easygem.business.service.imp;

import com.easygem.business.mapper.ClientConcernMapper;
import com.easygem.business.service.ClientConcernService;
import com.easygem.business.service.base.AbstractServiceImpl;
import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.business.BgClient;
import com.easygem.common.model.business.BgClientType;
import com.easygem.common.model.system.Employee;
import com.easygem.common.response.ActiveUser;
import com.easygem.common.vo.business.BgClientVO;
import com.easygem.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 廖思炜
 * @version 1.0
 * @className ClientConcernServiceImpl
 * @Description TODO 客户关怀业务层接口实现类
 * @date 2025/1/13 19:51
 */
@Service
public class ClientConcernServiceImpl extends AbstractServiceImpl<ClientConcernMapper,BgClient,Integer> implements ClientConcernService {
    @Autowired
    private ClientConcernMapper clientConcernMapper;
    /**
     * 根据条件分页得到客户关怀界面的信息
     *
     * @param bgClientConcernVO
     * @return
     */
    @Override
    public PageVO<BgClient> getClientConcerns(BgClientVO bgClientConcernVO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        BgClient bgClient = new BgClient();
        if(bgClientConcernVO!=null){
            BeanUtils.copyProperties(bgClientConcernVO,bgClient);
        }
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Employee employee = activeUser.getEmployee();
        if (employee.getType()!=0){
            bgClient.setEmployeeIdAftermarket(employee.getEmpId());
        }
        List<BgClient> bgClients=clientConcernMapper.getClientConcerns(bgClient);
        PageInfo<BgClient> pageInfo=new PageInfo<>(bgClients);
        return new PageVO<>(pageInfo.getTotal(),bgClients);
    }

    /**
     * 查询所有客户类型并返回
     *
     * @return
     */
    @Override
    public List<BgClientType> getAllClientTypes() {
        return clientConcernMapper.getAllClientTypes();
    }

    /**
     * 修改关注状态
     * @param bgClientConcernVO
     * @throws Exception
     */
    @Override
    public void updateAttention(BgClientVO bgClientConcernVO) throws Exception {
        BgClient bgClient=clientConcernMapper.selectByPrimaryKey(bgClientConcernVO.getClientId());

        if (bgClient == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"修改的账户信息不存在");
        }
        if (bgClientConcernVO.getIsAttention().equals(bgClient.getIsAttention())) {
            return;
        }
        if (
                bgClientConcernVO.getIsAttention()!=null
                &&bgClientConcernVO.getIsAttention()>-1
                &&bgClientConcernVO.getIsAttention()<2
        ){
            bgClient.setIsAttention(bgClientConcernVO.getIsAttention());
        }else {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"参数错误");
        }

        clientConcernMapper.updateByPrimaryKeySelective(bgClient);
    }

    /**
     * 客户续签
     *
     * @param bgClientConcernVO
     */
    @Override
    public void renew(BgClientVO bgClientConcernVO) throws Exception {
        BgClient bgClient=clientConcernMapper.selectByPrimaryKey(bgClientConcernVO.getClientId());
        if (bgClient == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"操作的用户不存在");
        }
        if (bgClientConcernVO.getBalance()==null||bgClientConcernVO.getBalance()<0) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"参数不合法");
        }
        if (bgClientConcernVO.getLastUpdatedDate()==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"参数不合法");
        }
        BeanUtils.copyProperties(bgClientConcernVO,bgClient);
        clientConcernMapper.renew(bgClient);
    }

    /**
     * 根据账户名得到应用key
     *
     * @param accountName
     * @return
     */
    @Override
    public String getAppIdByName(String accountName) {
        return clientConcernMapper.getAppIdByName(accountName);
    }

    /**
     * 根据账户名得到应用秘钥
     *
     * @param accountName
     * @return
     */
    @Override
    public String getAppSecretByName(String accountName) {
        return clientConcernMapper.getAppSecretByName(accountName);
    }

}
